/*
 * ClassGenerator.java
 *
 * Created on April 8, 2010, 9:28 AM
 *
 */

package com.absisgroup.nsc.utilities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
/**
 *
 * @author Hue
 */
public class ClassGenerator extends EntityGenerator {

    public ClassGenerator() {
        super();
    }

    public static void main(final String[] args) throws Exception {
        final String className = args[0];
        final String packageName = args[1];
        final String author = args[2];
        final String strQuery = args[3];
        final Connection con = getConnection();
        new ClassGenerator().generate(con, className, packageName, author, strQuery);
        con.close();
    }
    
    public byte[] generate(final Connection con, final String className, final String packageName, final String author, final String strQuery) throws Exception {
        final StringBuffer sbCols = new StringBuffer();
        final ArrayList alColumns = getColumns(con, strQuery, sbCols);
        final SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
        final StringBuffer sbImports = new StringBuffer("import com.absisgroup.util.ICustomEntity;\r\n");
        final String properties = generateProperties(alColumns, sbImports);
        final StringBuffer sb = new StringBuffer();
        sb.append("/*\r\n");
        sb.append(" * ").append(className).append(".java\r\n");
        sb.append(" *\r\n");
        sb.append(" * Generated on ").append(df.format(new java.util.Date())).append("\r\n");
        sb.append(" * DO NOT MODIFY THIS CLASS!!!. It's generated automatically.\r\n");
        sb.append(" */\r\n\r\n");

        sb.append("package ").append(packageName).append(";\r\n\r\n");
        sb.append(sbImports);
        sb.append("/**\r\n");
        sb.append(" *\r\n");
        sb.append(" * @author ").append(author).append("\r\n");
        sb.append(" */\r\n");
        sb.append("public class ").append(className).append(" implements ICustomEntity {\r\n");
        sb.append(properties);
        sb.append(generateMethods(alColumns, true));
        sb.append("\r\n");
        sb.append(mappingInfo(sbCols.toString()));
        sb.append('}');
        //storeFile("C:/" + className + ".java", sb.toString().getBytes());
        return sb.toString().getBytes();
    }
    
    private String mappingInfo(final String columns) {
        final String methods = "    public String getColumns() {\r\n" +
                         "        return \"" + columns + "\";\r\n" +
                         "    }\r\n";

        return methods;
    }
    
    private ArrayList getColumns(final Connection con, final String strQuery, final StringBuffer sbCols) throws Exception {
        final ArrayList al = new ArrayList();
        final PreparedStatement pStmt = con.prepareStatement(strQuery);
        final ResultSetMetaData metaData = pStmt.executeQuery().getMetaData();
        final int iCount = metaData.getColumnCount();
        for(int i=1; i<=iCount; i++) {
            al.add(new String[]{metaData.getColumnName(i), metaData.getColumnTypeName(i)});
            sbCols.append(i == 1 ? metaData.getColumnName(i) : "," + metaData.getColumnName(i));
        }
        pStmt.close();

        return al;
    }
}
